<?php

namespace app\index\controller;

use app\index\model\AlbumAudio as AlbumAudioModal;
use app\index\model\Album as AlbumModal;
use app\index\model\AlbumAudioLogo as AlbumAudioLogoModal;
use app\index\validate\Album as AlbumValidate;
use app\BaseController;
use app\Request;

class Album extends BaseController
{
    protected $middleware = ['Auth'];
    public function getPage()
    {
        $params = $this->request->post(['size', 'id']);
        $sql = new AlbumAudioModal();
        $count = $sql->where('id', '<=', $params['id'])->count();
        $size = $params['size'];
        $page = ceil($count / $size);
        return result()->success($page);
    }
    public function list(Request $request) {
        $params = $this->request->post(['current', 'size', 'name']);
        $validate = new AlbumValidate();
        if (!$validate->scene('list')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $this->userinfo = $request->user;
        $sql = new AlbumModal();
        $whereArr = [];
        if(isset($params['name']) && $params['name']) {
            $whereArr[] = ["name", "LIKE", '%'.$params['name'].'%'];
        }
        $list = $sql
            ->where($whereArr)
            ->order('sort asc')
            ->order('create_at desc')
            ->page($params['current'])
            ->with([
                'logInfo' => function($query) { $query->where('userId', $this->userinfo['id']); },
                'firstInfo',
            ])
            ->limit($params['size'])
            ->select();
        return result()->success($list);
    }

    public function chapter() {
        $params = $this->request->post(['superiorId','current', 'size', 'name']);
        $validate = new AlbumValidate();
        if (!$validate->scene('chapter')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new AlbumAudioModal();
        $list = $sql
            ->where('superiorId', $params['superiorId'])
            ->order('sort asc')
            ->order('create_at desc')
            ->page($params['current'])
            ->limit($params['size'])
            ->select();
        return result()->success($list);
    }

    public function detail(Request $request) {
        $params = $this->request->post(['id']);
        $this->userinfo = $request->user;
        $sql = new AlbumAudioModal();
        $info =  $sql
            ->where('id', $params['id'])
            ->with([
                'info',
                'logInfo' => function($query) { $query->where('userId', $this->userinfo['id']); },
            ])
            ->withCount('commentCount')
            ->find();
        return result()->success($info);
    }

    public function doLogList(Request $request) {
        $params = $this->request->post(['name','current', 'size']);
        $validate = new AlbumValidate();
        if (!$validate->scene('list')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $user = $request->user;
        $sql = new AlbumAudioLogoModal();
        $whereArr[] = ['userId', '=', $user['id']];
        $haswhereArr = [];
        if(isset($params['type']) && $params['type']) {
            $haswhereArr[] = ["name", "LIKE", '%'.$params['name'].'%'];
        }
        $list = $sql
            ->where($whereArr)
            ->hasWhere('info', $haswhereArr)
            ->page($params['current'])
            ->limit($params['size'])
            ->order('update_at desc')
            ->with(['info', 'musicInfo'])
            ->select();
        return result()->success($list);
    }

    public function doLog(Request $request) {
        $user = $request->user;
        $sql = new AlbumAudioLogoModal();
        $audioSql = new AlbumAudioModal();
        $list = $sql->where('userId', $user['id'])->order('create_at desc')->page(1)->limit(1)->select();
        if(count($list) > 0) {
            $info =  $audioSql->where('id', $list[0]['audioId'])->with('info')->withCount('commentCount')->find();
            $info->currenTime = $list[0]['time'];
            return result()->success($info);
        }
        return result()->success("");
    }

    public function addLog(Request $request) {
        $params = $this->request->post(['albumId', 'audioId', 'time']);
        $validate = new AlbumValidate();
        if (!$validate->scene('addLog')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $user = $request->user;
        $sql = new AlbumAudioLogoModal();
        $info = $sql->where([
            ['albumId', '=', $params['albumId']],
            ['userId', '=', $user['id']],
        ])->find();
        if(empty($info)) {
            $sql->save([
                'userId' => $user['id'],
                'albumId' => $params['albumId'],
                'audioId' => $params['audioId'],
                'time' => $params['time'],
            ]);
            return result()->success("添加记录成功");
        } else {
            $info->audioId = $params['audioId'];
            $info->time = $params['time'];
            $info->save();
            return result()->success("修改记录成功");
        }
    }

    public function pre() {
        $params = $this->request->post(['id']);
        $sql = new AlbumAudioModal();
        $info =  $sql->where('id', $params['id'])->find();
        if(empty($info)) {
            return result()->fail("此章节不存在", 400);
        }
        $list =  $sql
            ->where('superiorId', $info['superiorId'])
            ->where('sort', '<', $info['sort'])
            ->with('info')
            ->order('sort desc')
            ->order('create_at desc')
            ->page(1)
            ->limit(1)
            ->select();
        return result()->success($list);
    }
    public function next() {
        $params = $this->request->post(['id']);
        $sql = new AlbumAudioModal();
        $info =  $sql->where('id', $params['id'])->find();
        if(empty($info)) {
            return result()->fail("此章节不存在", 400);
        }
        $list =  $sql
            ->where('superiorId', $info['superiorId'])
            ->where('sort', '>', $info['sort'])
            ->with('info')
            ->order('sort asc')
            ->order('create_at desc')
            ->page(1)
            ->limit(1)
            ->select();
        return result()->success($list);
    }
}